package com.shanghaionstar.presenter;

import android.support.v4.view.ViewPager;
import android.support.v4.widget.EdgeEffectCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.common.util.PreferencesUtls;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.sqlite.entities.LandingsPageEntity;
import com.onstar.cn.sqlite.entities.LandingsPageEntityNew;
import com.onstar.cn.sqlite.xutil.exception.DbException;
import com.shanghaionstar.OnstarApplication;
import com.shanghaionstar.R;
import com.shanghaionstar.iview.ILoadingView;

import com.shanghaionstar.service.LandingPageService;

import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 7828wc on 2016/6/22.
 */
public class LoadingPresenter extends BasePresenter<LandingsPageEntityNew> implements LandingPageService.ResultListener {
    private ILoadingView<LandingsPageEntityNew> loadingView;
//    private LandingPageDBService landingPageDBService;
    private LogManager log = LogManager.getInstance();
    private boolean isShowLandingPage = false;
    private LandingsPageEntityNew landingPage;
    private LandingPageService landingPageService;
    private String downloadUrl = "";
    private String filePath = "";
    private boolean isStop = false;


    public LoadingPresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<LandingsPageEntityNew> view) {
        super.setView(view);
        this.loadingView = (ILoadingView<LandingsPageEntityNew>) view;
        landingPageService = new LandingPageService(loadingView.onActivity());
//        landingPageDBService = new LandingPageDBService(loadingView.onActivity().getApplicationContext());
        super.setServiceProvider(landingPageService);
    }


    public void loadDBLandingPage() {
        try {
            landingPage = OnstarApplication.getmDbUtils().findFirst(LandingsPageEntityNew.class);
//            landingPageDBService.getLandingPage();
        } catch (DbException e) {
            e.printStackTrace();
        }
//
        if (landingPage != null) {
            File localFile = new File(landingPage.getFilePath());
            if (localFile.exists()) {
                loadingView.showLandingPage(landingPage);
                log.d("Get local file success,waiting for view refresh");
            } else {
                try {
                    OnstarApplication.getmDbUtils().delete(landingPage);
                } catch (DbException e) {
                    e.printStackTrace();
                }
//                landingPageDBService.delete(landingPage.getId());
            }
        }
    }

    //引导页相关

    private Integer[] pics = {R.drawable.loading_guide_page1, R.drawable.loading_guide_page2,
            R.drawable.loading_guide_page3, R.drawable.loading_guide_page4, R.drawable.loading_guide_page5};

    EdgeEffectCompat leftEdge, rightEdge;

    public void getEdge(ViewPager vp) {
        try {
            Field leftEdgeField = vp.getClass().getDeclaredField("mLeftEdge");
            Field rightEdgeField = vp.getClass().getDeclaredField("mRightEdge");
            if (leftEdgeField != null && rightEdgeField != null) {
                leftEdgeField.setAccessible(true);
                rightEdgeField.setAccessible(true);
                leftEdge = (EdgeEffectCompat) leftEdgeField.get(vp);
                rightEdge = (EdgeEffectCompat) rightEdgeField.get(vp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void initData() {
        List<View> views = new ArrayList<>();
        ViewGroup.LayoutParams mParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        for (int i = 0; i < pics.length - 1; i++) {
            ImageView iv = new ImageView(view.onActivity());
            iv.setLayoutParams(mParams);
            iv.setImageResource(pics[i]);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            views.add(iv);
        }
        View mView = LayoutInflater.from(loadingView.onActivity()).inflate(R.layout.onstar_loading_experience, null);
        mView.setBackgroundResource(R.drawable.loading_guide_page5);
        views.add(mView);
        loadingView.setupViewPager(views);
    }

    public boolean onPageScrolled() {
        if (rightEdge != null && !rightEdge.isFinished()) {//到了最后一张并且还继续拖动，出现蓝色限制边条了
            PreferencesUtls.getInstance(view.onActivity()).putBoolean(Constants.LOADING_GUIDE, true);
            return true;
        }
        return false;
    }

    @Override
    public void onSuccess(LandingsPageEntityNew landingPage) {
        try {
            OnstarApplication.getmDbUtils().saveBindingId(landingPage);
        } catch (DbException e) {
            e.printStackTrace();
        }
//        landingPageDBService.insert(landingPage);
    }

    @Override
    public void onError(Exception e) {

    }

    @Override
    public void finish() {
        super.finish();
        landingPageService.setResultListener(null);
        landingPageService = null;
//        landingPageDBService = null;
    }
}

